package com.heima.bos.web.action.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.heima.bos.domain.Region;
import com.heima.bos.domain.Subarea;
import com.heima.bos.service.BaseService;
import com.heima.bos.service.SubareaService;
import com.heima.bos.utils.FileUtils;
import com.heima.bos.web.action.SubareaActionInter;
@SuppressWarnings("all")
@Controller
@Scope("prototype")
public class SubareaAction extends BaseAction<Subarea> implements SubareaActionInter{
	@Resource
	private SubareaService subareaService;
	@Override
	protected BaseService<Subarea> getService() {
		return subareaService;
	}
	/**
	 * 为添加定区表单选项提供数据
	 */
	public String toDecidezoneSelect(){
		
		DetachedCriteria detachedCriteria = pageBean.getDetachedCriteria();
		detachedCriteria.add(Restrictions.isNull("decidezone"));
		List<Subarea> list = subareaService.findByCriteria(detachedCriteria);
		
		String[] excludes = new String[]{"decidezone","region","startnum","endnum","single"};
		writeList2Json(list, excludes);
		return NONE;
	}
	/**
	 * 分页查询
	 */
	public String pageQuery(){
		DetachedCriteria subCriteria = pageBean.getDetachedCriteria();
		String addresskey = entity.getAddresskey();//关键字
		Region region = entity.getRegion();
		if(StringUtils.isNotBlank(addresskey)){
			subCriteria.add(Restrictions.like("addresskey", "%"+addresskey+"%"));
		}
		if(region != null){
			DetachedCriteria regCriteria = subCriteria.createCriteria("region");
			String province = region.getProvince();
			String city = region.getCity();
			String district = region.getDistrict();
			
			if(StringUtils.isNotBlank(province)){
				regCriteria.add(Restrictions.like("province", "%"+province+"%"));
			}
			if(StringUtils.isNotBlank(city)){
				regCriteria.add(Restrictions.like("city", "%"+city+"%"));
			}
			if(StringUtils.isNotBlank(district)){
				regCriteria.add(Restrictions.like("district", "%"+district+"%"));
			}
			
		}
		pageList();
		String[] excludes = new String[]{"page","pageSize","detachedCriteria","subareas","decidezone"};
		writePageBean2Json(pageBean, excludes);
		return NONE;
	}
	/**
	 * 导出xls数据文件
	 */
	public String exportXls(){
		try {
			//获取所有分区数据
			List<Subarea> list = subareaService.findAll();
			//创建xls
			HSSFWorkbook work = new HSSFWorkbook();
			//创建sheet
			HSSFSheet sheet = work.createSheet("分区数据");
			//创建第一行
			HSSFRow row = sheet.createRow(0);
			row.createCell(0).setCellValue("分区编号");
			row.createCell(1).setCellValue("区域编码");
			row.createCell(2).setCellValue("关键字");
			row.createCell(3).setCellValue("起始号");
			row.createCell(4).setCellValue("结束号");
			row.createCell(5).setCellValue("单双号");
			row.createCell(6).setCellValue("位置信息");
			//循环依次创建行和列
			for (Subarea area : list) {
				//创建行
				HSSFRow nextRow = sheet.createRow(sheet.getLastRowNum()+1);
				//给列赋值
				nextRow.createCell(0).setCellValue(area.getId());
				nextRow.createCell(1).setCellValue(area.getRegion().getId());
				nextRow.createCell(2).setCellValue(area.getAddresskey());
				nextRow.createCell(3).setCellValue(area.getStartnum());
				nextRow.createCell(4).setCellValue(area.getEndnum());
				nextRow.createCell(5).setCellValue(area.getSingle());
				nextRow.createCell(6).setCellValue(area.getPosition());
			}
			//获取输出流
			ServletOutputStream out = ServletActionContext.getResponse().getOutputStream();
			String fileName = "分区数据.Xls";
			String mimeType = ServletActionContext.getServletContext().getMimeType(fileName);
			//中文乱码
			fileName = FileUtils.encodeDownloadFilename(fileName, ServletActionContext.getRequest().getHeader("User-Agent"));
			System.out.println(fileName);
			//两个头
			ServletActionContext.getResponse().setContentType(mimeType);
			ServletActionContext.getResponse().setHeader("content-disposition", "attachment;filename="+fileName);
			//写出
			work.write(out);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return NONE;
	}
}
